package com.hsw.t5;

import java.util.Random;

public class Test {
    public static void main(String[] args) {
        String s=getCaptcha();
        System.out.println(s);
    }

    private static String getCaptcha() {
        StringBuilder sb = new StringBuilder();
        //拼接a-z
        StringBuilder sb1 = splicing(sb, 'a', 'z');
        StringBuilder sb2 = splicing(sb1, 'A', 'Z');
        StringBuilder sb3 = splicing(sb2, '0', '9');
        //将可变字符串转为字符串
        String s = sb3.toString();
        Random r=new Random();
        StringBuilder ssb = new StringBuilder();
        //从拿到的字符串中取出
        for (int i = 0; i < 4; i++) {
            ssb.append(s.charAt(r.nextInt(s.length()-1)));
        }
        return ssb.toString();
    }
    //拼接字符串方法
    public static StringBuilder splicing(StringBuilder sb,char c1,char c2){
        if(c1>c2){
           throw new RuntimeException("theMethodIsUsedIncorrectly");
        }
        while(true){
            sb.append(c1);
            if(c1==c2){
                return sb;
            }
            c1=(char)(c1+1);
        }
    }
}
